import {orderDetail, changeOrderInfo, orderSendGoods} from '../services/api';
import {message} from 'antd';

export default {
    namespace: 'orderDetail',

    state: {
        status_records: [],
        specs_list: []
    },

    effects: {
        *fetch({payload}, {call, put}) {
            const response = yield call(orderDetail, payload);
            yield put({
                type: 'save',
                payload: response,
            });
        },
        *changeInfo({payload}, {call, put}) {
            const response = yield call(changeOrderInfo, payload);
            if (response) {
                message.success('成功');
                yield put({
                    type: 'saveChangeInfo',
                    payload: payload,
                });

            } else {
                message.error('失败');
            }
        },
        *sendGoods({payload}, {call, put}) {
            const response = yield call(orderSendGoods, payload);
            if (response) {
                message.success('成功');
                yield put({
                    type: 'saveSendGoods',
                    payload: payload,
                });
            } else {
                message.error('失败');
            }
        },
    },

    reducers: {
        save(state, {payload}){
            return {
                ...state,
                ...payload
            }
        },
        saveChangeInfo(state, {payload}){
            return {
                ...state,
                ...payload
            }
        },
        saveSendGoods(state, {payload}){
            let goods_list = [...state.goods_list];
            const {ids, express, express_no} = payload;
            const idsArr = ids.split(',');
            goods_list.forEach(v=> {
                idsArr.forEach(id=> {
                    if (id == v.id) {
                        v.status = 103;
                        v.express = express;
                        v.express_order = express_no;
                    }
                })
            });
            return {
                ...state,
                goods_list,
            }
        },
        clear(state, { payload }){
            return {
                status_records: [],
                specs_list: []
            }
        }
    },
};
